import { Tag } from 'server/models/Tag'
import { successResponse, errorResponse } from 'server/utils/response'

export default defineEventHandler(async (event) => {
  try {
    const tags = await Tag.getTagsWithPostCount()
    
    // 转换为前端需要的格式
    const formattedTags = tags.map(tag => ({
      id: tag.id?.toString() || '',
      name: tag.name,
      slug: tag.slug,
      color: tag.color || '#1890ff',
      postCount: tag.post_count || 0,
      status: tag.post_count && tag.post_count > 0 ? 'active' : 'inactive',
      createdAt: tag.created_at ? new Date(tag.created_at).toISOString().split('T')[0] : '',
      updatedAt: tag.updated_at ? new Date(tag.updated_at).toISOString().split('T')[0] : ''
    }))

    return successResponse(formattedTags, '获取标签列表成功')
  } catch (error) {
    console.error('获取标签列表失败:', error)
    return errorResponse('获取标签列表失败', error instanceof Error ? error.message : '未知错误')
  }
})